<?php

namespace App\Http\Controllers\Admin;

use App\Model\Permission;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Redis;

class PermissionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // 存放权限列表数组
        $per_list = [];
        // 实例化redis
        $Redis = new Redis();
        $Redis->connect('localhost', 6379);
        // 权限id
        $listkey = 'LIST:Permission';
        // 权限内容
        $hashkey = 'HASH:Permission:';
        // 从redis中读取分类 存在
        if($Redis->exists($listkey)){
            // 存放所有分类id
            $lists = $Redis->lRange($listkey,0,-1);
            foreach ($lists as $k=>$v){
                $per_list[] = $Redis->hGetAll($hashkey.$v);
            }
        }else{
            $per_list = Permission::get()->toArray();
            // 将数据存入redis
            foreach ($per_list as $v){
                // 将分类id存放在$listkey当中
                $Redis->rPush($listkey,$v['id']);
//                 将分类存放到$hashkey
                $Redis->hMSet($hashkey.$v['id'],$v);
            }
        }

        return view('admin.permission.list',compact('per_list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.permission.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 1.获取用户添加的数据  role_name
        $input = $request->all();

        $per_name = Permission::where('per_name',$input['per_name'])->first();

        if($per_name){
            $data = [
                'code'=> 0,
                'message'=>'该权限名已经存在'
            ];
            return $data;
        }

        // 封装插入数据库的数组
        $data = [
            'per_name' => $input['per_name'],
            'per_url' => $input['per_url'],
        ];

        // 验证
        $result = Permission::create($data);

        if($result === FALSE){
            $data = [
                'code'=> 0,
                'message'=>'添加失败'
            ];
        }else{
            $data = [
                'code'=> 1,
                'message'=>'添加成功'
            ];
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $permission = Permission::find($id);
        return view('admin.permission.edit',compact('permission'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // 查询
        $permission = Permission::find($id);

        // 获取修改的数据
        $per_name = $request->input('per_name');
        $per_url = $request->input('per_url');

        $data= [
            'per_name' => $per_name,
            'per_url' => $per_url,
        ];

        $result = $permission->update($data);
        if($result === FALSE){
            $data = [
                'code'=> 0,
                'message'=>'修改失败'
            ];
        }else{
            $data = [
                'code'=> 1,
                'message'=>'修改成功'
            ];
        }
        return $data;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $permission = Permission::find($id);

        $result = $permission->delete();

        if($result === FALSE){
            $data = [
                'code'=> 0,
                'message'=>'删除失败'
            ];
        }else{
            $data = [
                'code'=> 1,
                'message'=>'删除成功'
            ];
        }

        return $data;
    }

    public function delAll(Request $request)
    {
        $ids = $request->input('ids');

        $result = Permission::destroy($ids);

        if($result === FALSE){
            $data = [
                'code'=> 0,
                'message'=>'删除失败'
            ];
        }else{
            $data = [
                'code'=> 1,
                'message'=>'删除成功'
            ];
        }

        return $data;

    }
}
